const postsData = require('../../../data/index-data.js');
const app = getApp();
Page({
    data: {
        isPlayingMusic: false,
        pageId:null
    },
    onLoad(options) {
        const postId   = options.id;
        this.getPostData(postId);
        this.initCollected(postId);
        this.onWatchMusicStatu();
    },
    getPostData(postId) {
        //文章内容
        const postItem = postsData.postList[postId];
        this.setData({
            postData: postItem,
            currentPostId: postId
        });
    },
    initCollected(postId) {
        //收藏状态
        let postsCollected = wx.getStorageSync("post-collected");
        if (postsCollected) {
            let postCollected = postsCollected[postId];
            if (postCollected) {
                this.setData({
                    collected: postCollected
                })
            }
        } else {
            let postsCollected = {};
            postsCollected[postId] = false;
            wx.setStorageSync("post-collected", postsCollected);
        }
    },
    onCollectedTap() {
        //收藏
        let postsCollected = wx.getStorageSync("post-collected");
        let postCollected = postsCollected[this.data.currentPostId];
        postCollected = !postCollected;
        postsCollected[this.data.currentPostId] = postCollected;
        this.showToastModel(postsCollected, postCollected);
    },
    showToastModel(postsCollected, postCollected) {
        //收藏tip
        this.setData({
            collected: postCollected
        })

        wx.setStorageSync("post-collected", postsCollected);

        wx.showToast({
            title: postCollected ? "收藏成功" : "取消收藏",
            icon: 'success',
        });
    },
    onShareTap() {
        //分享
        wx.showActionSheet({
            itemList: [
                "分享到微信好友",
                "分享到朋友圈",
                "分享到QQ好友",
                "分享到微博"
            ],
            success(res) {
                console.log(res.tapIndex);
            }
        })
    },
    onMusicTap(event) {
        //音乐
        let isPlayingMusic = this.data.isPlayingMusic;
        let currentPostId  = this.data.currentPostId;
        let postMusic      = postsData.postList[currentPostId].music;
        
        if (isPlayingMusic) {
            wx.pauseBackgroundAudio();
        } else {
            wx.playBackgroundAudio({
                dataUrl: postMusic.url,
                title: postMusic.title,
                coverImgUrl: postMusic.coverImg,
            })
        }
        this.setData({ 
            isPlayingMusic: !isPlayingMusic,
        })
    },
    onWatchMusicStatu() {
        let postId = this.data.currentPostId;
        let pageId = app.gloabData.g_currentPageId;
        if (app.gloabData.g_isPlayingMusic && pageId == postId) {
            this.setData({ isPlayingMusic: true })
        }
        //监听播放
        wx.onBackgroundAudioPlay(() => {
            this.setData({ isPlayingMusic: true});
            app.gloabData.g_isPlayingMusic = true;
            app.gloabData.g_currentPageId  = postId;
        });
        wx.onBackgroundAudioPause(() => {
            this.setData({ isPlayingMusic: false})
            app.gloabData.g_isPlayingMusic = false;
            app.gloabData.g_currentPageId  = null;
        });
    }
})